<?php
// 	restituisce un array da passare a get_posts/query_posts a partire dal source
// 	la source va strutturata in questo modo
//
//	key=campo1,campo2,campo3&key2=campo4,campo5,campo6
//
//	key pu� assumere i valori
// 		type			post_type
//		status			post_status
//		author			post_author
//		_tax			tassonomia tax
//		customfield		custom_field

//	da fare ancora
//		upload_year		(post)
//		upload_month	(post)
//		expiration_year	(su custom)
//		expiration_month(su custom)
//		assigned_to		(post - content)
			
// 	nota: vorrei che 
//	se scrivo tag=a,b seleziono i post che hanno come tag a oppure b;
//	se scrivo tag=a&tag=b seleziono i post che hanno tag a e tag b;
//	prima questo funzionava, ora no

function fascicoli_converti_source_array($source, $groupby, $orderby = 'date', $order="ASC") {
	global $wpdb;
	global $post;
	
	if ($orderby == '') {
		$orderby = 'date';
	}
    if ($groupby == '') {
        $groupby= 'post_type';
    }
    // manca ancora l'ordinamento per gruppo

	// se non è nullo, devo interpretarla
	if ($source != '') {
		$and_keys = explode('&', $source);
		
		// set query
		$query_array = array();
		$query_array_all = array();
		$query_array_all['tax_query'] = array();
		$query_array_all['meta_query'] = array();
		$query_array_all['meta_query']['relation'] = 'AND';
		$query_array_all['tax_query']['relation'] = 'AND';		
		$query_array_all['post_status'] = 'any';				
		$query_array_all[] = $query_array;
			
		foreach($and_keys as $and_key) {			
			// tag=a&tag=b&tag=a,b&_view=ciao, ciccio&post_status=publish&fascicoli_code=azz&post_type=post
			// tag=a AND tag=b AND tag=(a OR b)
			// quindi tag=a+b+a,b			
			// $and_key = 'tag=a' oppure 'tag=b' oppure 'tag=a,b'
			// scompone $and_key ed inserisce i valori in $key e $string_values			
			$query_array = array();
			list($key, $string_values) = explode('=', $and_key);
			$or_values = explode(',', $string_values);
				
			switch ($key) {
				case 'post_status' :
					$query_array_all['post_status'] = $or_values;						
				break;
				
				case 'post_type' :
					$query_array_all['post_type'] = $or_values;					
				break;
				
				case 'post_parent':
					$query_array_all['post_parent'] = get_post_by_name($or_values);
				break;
				
				case 'author' :
					$query_array_all['author_name'] = $or_values;
				break;
				
				case 'tag' :
					$query_array_all['tag'][] = $or_values;
				break;				
				
				case 'publish_year':
					$query_array_all['year'] = $or_values;	
				break;
				
				case 'publish_month':
					$query_array_all['monthnum'] = $or_values;	
				break;
				
				case 'publish_day':
					$query_array_all['day'] = $or_values;	
				break;				
				
				//@TODO vanno implementate in qualche modo anche le date della scadenza

				/*
				case 'fascicoli_code' :
					$query_array_all['meta_query'][] = array(
						'key' => $key,
						'value' => $or_values,
						'compare' => 'IN',
					);	
				break;
				
				case 'revision':
					$query_array_all['meta_query'][] = array(
						'key' => $key,
						'value' => $or_values,
						'compare' => 'IN',
					);
					//@TODO non implementa "_CURRENT_"
				break;	
				*/		
				
				default:
					if (substr($key,0,1) == '_') {
						//� una tassonomia
						$key = substr($key, 1);
						$query_array_all['tax_query'][] = array(
							'taxonomy' => $key,
							'field' => 'slug',
							'terms' => $or_values,
							'include_children' => true,
							'operator' => 'IN'  
						);
					} else {
						// � un customfield
						$query_array_all['meta_query'][] = array(
							'key' => $key,
							'value' => $or_values,
							'compare' => 'IN',
						);						
					}
				break;
			}
			
			if (count($query_array)>0) {
				$query_array_all[] = $query_array;		
			}				
		}		
							
	} else {
        //@TODO per adesso, se non formisco source vuol dire che visualizzo gli allegati al fascicolo
		$query_array_all['post_type'] = 'attachment';
		$query_array_all['post_parent'] = $post->ID;		
	}	


	if (in_array($orderby, array('date', 'author', 'parent', 'status', 'type', 'title'))) {
		$query_array_all['orderby'] = $groupby.' '.$orderby;
	} else {		
		$query_array_all['orderby'] = $groupby.' '.'meta_value';
        $query_array_all['meta_key'] = $orderby;
	}

	if ($orderby['order']) {
		$query_array_all['order'] = $order;
	}
		

	$query_array_all['nopaging'] = true;	

	$array_tag_and = array();
	$array_tag_in = array();
	
	if (!empty($query_array_all['tag'])) {
		foreach($query_array_all['tag'] as $tag) {
			if (count($tag)==1) {
				$array_tag_and[] = $tag[0];
			} else {
				foreach($tag as $t) {
					$array_tag_in[] = $t[0];
				}
			}
		}
	}
	
	$query_array_all['tag_slug__and'] = $array_tag_and;	
	$query_array_all['tag_slug__in'] = $array_tag_in;	
	unset($query_array_all['tag']);
	
	return $query_array_all;
}



// modifica il testo di informazione in base allo stato
function fascicoli_fascicolo_infotext($post) {
	if (($post->post_status == 'draft') || ($post->post_status == 'pending') || ($post->post_status == 'future')) {
		return "(rev.{$post->revision} del {$post->post_date} in bozza)";
	} else {
		return "(rev.{$post->revision} del {$post->post_date} approvata da ".get_userdata($post->post_author)->user_login.")";
	}
}


function fascicoli_get_tasks($post) {
	global $task_posts;
	
	$tasks = array();
	$icons = '';
	$forms = '';
	
	$task_array = $task_posts[$post->post_type];	
	
	if (count($task_array) > 0) {		
		foreach ($task_array as $task=>$item) {			
			$icons .= '<img onClick="toggle_task(\'post_plus\','.$post->ID.')" src="'.$item['ico'].'"/ >';	
			$forms .= '<div id="'.$post->post_type.'_'.$task.'_form_'.$post->ID.'" class="fascicoli_form_'.$post->post_type.' fascicoli_form_task_div">'.apply_filters($post->post_type.'_'.$task.'_form', '').'</div>';	
		}
	}	
	
	$tasks[$post->ID]['icons'] = $icons;
	$tasks[$post->ID]['forms'] = $forms;
	
	return $tasks;
}



function fascicoli_post_plus_form_task() {
	
	$form = '';
	
	$form .= '<form class="fascicoli_task_form" name="post_plus_form" action="" method="post">';
		$form .= '<input type="hidden" name="fascicoli_task_form" value="go">';
		$form .= '<p class="fascicoli_form_line">';
		$form .= '<label for="Label 1" class="fascicoli_form_label">post_plus_form_input</label>';
		$form .= '<input type="text" name="post_plus_form_input" class="fascicoli_form_input">';
		$form .= '</p>';
		
		$form .= '<p class="post_plus_form_input">'.'<input type="submit" class="fascicoli_form_submit" name="post_plus_form_submit" name="Submit">'.'</p>';
	$form .= '</form>';
	
	return $form;
}



function post_plus_action_callback($post) {		
	//@TODO implementare azione		
	$message = apply_filters('post_plus_message', $post['post_plus_form_input']);
	echo $message;
}


function fascicoli_post_plus_message_task($message) {
	return '<h4>'.$message.'</h4>';	
}


